<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>3.天气案例_立即监视</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
		<style>
			img {
				width: 100px;
			}
		</style>
	</head>
	<body>
		<!-- 准备好一个容器-->
		<div id="demo">
			<h2>今天天气很{{text}}</h2>
			<img :src="url"> <br>
			<button @click="changeWeather">切换天气</button>
		</div>

		<script type="text/javascript">
			new Vue({
				el:'#demo',
				data:{
					isHot:true,
					url:'http://49.232.112.44/images/hot.jpg'
				},
				computed:{
					text(){
						return this.isHot ? '炎热' : '凉爽'
					}
				},
				methods:{
					changeWeather(){
						this.isHot = !this.isHot
					}
				},
				watch:{
					isHot:{
						immediate:true, //控制是否开启立即监视，开启后第一次调用oldValue的值为undefined
						handler(newValue,oldValue){
							console.log('isHot的值发生了变化',newValue,oldValue)
							if(newValue){
								this.url = 'http://49.232.112.44/images/hot.jpg'
							}else{
								this.url = 'http://49.232.112.44/images/cool.jpg'
							}
						}
					},
				}
			})
		</script>
	</body>
</html>